<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2013-08-19 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'mobiapi/modules/base.mod.php';
require ROOT_PATH."service/story.service.php";

class ModuleObj extends BaseModule
{
	private $service;

	function __construct($request)
	{
		parent::__construct($request);

		$this->service = new StoryService();

		$this->excute();
	}

	function excute()
	{
		switch($this->act)
		{
			case 'new':
				$this->getNewStoryList();
				break;
			case 'hot':
				$this->getHotStoryList();
				break;
			case 'followList':
				$this->getFollowStoryList();
				break;
			case 'list':
				$this->getStoryList();
				break;
			case 'info':
				$this->getStoryInfo();
				break;
			case 'add':
				$this->addStory();
				break;
			case 'delete':
				$this->deleteStory();
				break;
			case 'update':
				$this->updateStory();
				break;
			case 'search':
				$this->searchStory();
				break;
			case 'likeList':
				$this->getStoryLikeList();
				break;
			case 'commentList':
				$this->getStoryCommentList();
				break;
			case 'addLike':
				$this->addLike();
				break;
			case 'addComment':
				$this->addComment();
				break;
			case 'clearOrder':
				$this->clearOrder();
				break;
			case 'setOrder':
				$this->setOrder();
				break;
			default:
				$this->message('ACTION_NOT_EXISTS', STATUS_E);
		}

		$this->showResponse();
	}

	function getNewStoryList()
	{
		$this->request['uid'] = $this->user['id'];
		$result = $this->service->get_new_list($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}

	function getHotStoryList()
	{
		$this->request['uid'] = $this->user['id'];
		$result = $this->service->get_hot_list($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}

	function getFollowStoryList()
	{
		$this->request['uid'] = $this->user['id'];
		$this->request['id'] = $this->user['id'];
		$result = $this->service->get_list_by_follow($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}

	function getStoryList()
	{
		//$this->checkRequest('list');

		$this->request['uid'] = $this->user['id'];
		if (!isset($this->request['id'])) $this->request['id'] = $this->user['id'];
		$result = $this->service->get_list_by_uid($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}

	function getStoryInfo()
	{
		$this->checkRequest('info');

		$result = $this->service->get($this->user['id'], $this->request['id']);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['info'] = $result;
	}

	function getStoryLikeList()
	{
		$this->checkRequest('info');

		$result = $this->service->get_like_list($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result);
		$this->response['list'] = $result;
	}

	function getStoryCommentList()
	{
		$this->checkRequest('info');

		$this->request['uid'] = $this->user['id'];
		$this->request['sid'] = $this->request['id'];
		$result = $this->service->get_comment_list($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}

	function addStory()
	{
		$this->checkRequest('story_info');

		$last_add = $this->service->get_last_add($this->user['id']);
		if (($last_add+60) > time()) $this->message('PLEASE_WAIT', STATUS_F);

		$this->request['uid'] = $this->user['id'];
		$result = $this->service->add($this->request);

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}

	function deleteStory()
	{
		$this->checkRequest('info');

		if (stripos($this->user['email'], 'nikm') !== false 
			|| stripos($this->user['email'], 'oyeii1314@163.com') !== false 
			|| stripos($this->user['email'], 'wangjiangbo1314@163.com') !== false)
		{
			$info = $this->service->get('', $this->request['id']);
			if (check_array($info))
			{
				$this->request['uid'] = $info['user_id'];
			}
		}
		else 
		{
			$this->request['uid'] = $this->user['id'];
		}
		
		$result = $this->service->delete($this->request);

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}

	function updateStory()
	{
		$this->checkRequest('story_info');

		$result = $this->service->update($this->request);

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}

	function searchStory()
	{
		$this->checkRequest('name_search');

		$this->request['title'] = $this->request['name'];
		$result = $this->service->search($this->request);

		if (!check_array($result)) $this->message('NO_RESULT', STATUS_F);

		$this->response['size'] = (string) count($result['list']);
		$this->response['list'] = $result['list'];
		$this->response['total_count'] = $result['total_count'];
	}

	function addLike()
	{
		$this->checkRequest('info');

		$this->request['uid'] = $this->user['id'];
		$this->request['sid'] = $this->request['id'];

		if ($this->service->check_like($this->request)) $this->message('ALREADY_LIKE', STATUS_F);

		$result = $this->service->add_like($this->request);

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}

	function addComment()
	{
		$this->checkRequest('comment_info');

		$this->request['uid'] = $this->user['id'];
		$this->request['sid'] = $this->request['id'];

		if ($this->service->check_comment($this->request)) $this->message('ALREADY_COMMENT', STATUS_F);

		$result = $this->service->add_comment($this->request);

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}

	function clearOrder()
	{
		if (stripos($this->user['email'], 'nikm') === false 
			&& stripos($this->user['email'], 'oyeii1314@163.com') === false 
			&& stripos($this->user['email'], 'wangjiangbo1314@163.com') === false)
		{
			$this->message('ACCESS_DENIED', STATUS_F);
		}

		$result = $this->service->clear_order();

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}

	function setOrder()
	{
		if (stripos($this->user['email'], 'nikm') === false 
			&& stripos($this->user['email'], 'oyeii1314@163.com') === false 
			&& stripos($this->user['email'], 'wangjiangbo1314@163.com') === false)
		{
			$this->message('ACCESS_DENIED', STATUS_F);
		}
		
		$this->checkRequest('order_set');

		$result = $this->service->set_order($this->request);

		$this->response['status'] = $result ? STATUS_S : STATUS_F;
	}
}